package com.sleepcamel.resteasyConfigurator.view.detail;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.fieldassist.AutoCompleteField;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.wb.swt.SWTResourceManager;

import com.sleepcamel.rest.domain.BaseMethod;
import com.sleepcamel.rest.domain.method.parameter.MethodParameter;
import com.sleepcamel.resteasyConfigurator.util.ClassProvider;
import com.sleepcamel.resteasyConfigurator.view.updateValueStrategies.ClassToStringUpdateValueStrategy;
import com.sleepcamel.resteasyConfigurator.view.updateValueStrategies.StringToClassUpdateValueStrategy;

abstract public class MethodDetailView<T extends BaseMethod<MethodParameter>> extends DetailView<T> {

	protected final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	private Text txtMethodText;
	private Text txtReturnType;
	
	private Label lblMethodName;
	private Label lblReturnType;
	protected SashForm sashForm;
	private Label lblParameters;
	
	protected MethodParameterView<?> methodParameterView;
	private MethodParameterViewList parametersComposite;
	protected Composite firstComposite;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public MethodDetailView(final Composite parent, int style) {
		super(parent, SWT.NONE);
		setLayout(new FillLayout(SWT.HORIZONTAL));

		final ScrolledComposite scrollComposite = new ScrolledComposite(this,
				SWT.V_SCROLL | SWT.BORDER);

		sashForm = new SashForm(scrollComposite, SWT.VERTICAL);
		scrollComposite.setContent(sashForm);
		scrollComposite.setExpandVertical(true);
		scrollComposite.setExpandHorizontal(true);
		scrollComposite.setMinSize(560, 390);
		
		firstComposite = formToolkit.createComposite(sashForm, SWT.NONE);
		TableWrapLayout twl_composite = new TableWrapLayout();
		twl_composite.numColumns = 4;
		firstComposite.setLayout(twl_composite);

		formToolkit.paintBordersFor(firstComposite);
		
		lblMethodName = formToolkit.createLabel(firstComposite, "Name", SWT.NONE);
		lblMethodName.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 1));
		lblMethodName.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		txtMethodText = formToolkit.createText(firstComposite, "", SWT.NONE);
		txtMethodText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		txtMethodText.setMessage("Method Name");
		
		lblReturnType = formToolkit.createLabel(firstComposite, "Return type", SWT.NONE);
		lblReturnType.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 1));
		lblReturnType.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		txtReturnType = formToolkit.createText(firstComposite, "New Text", SWT.NONE);
		new AutoCompleteField(txtReturnType, new TextContentAdapter(), ClassProvider.getInstance().getClassesNames());

		txtReturnType.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		
		lblParameters = formToolkit.createLabel(firstComposite, "Parameters", SWT.NONE);
		lblParameters.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));

		parametersComposite = new MethodParameterViewList(firstComposite, SWT.NONE);
		TableWrapData twd_parametersComposite = new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 3);
		twd_parametersComposite.heightHint = 109;
		parametersComposite.setLayoutData(twd_parametersComposite);
		formToolkit.paintBordersFor(parametersComposite);
		firstComposite.pack();
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = super.initDataBindings();
		//
		IObservableValue methodNameObserveValue = BeansObservables.observeValue(modelData, "name");
		IObservableValue txtNameObserveTextObserveWidget = SWTObservables.observeText(txtMethodText, SWT.Modify);
		bindingContext.bindValue(txtNameObserveTextObserveWidget, methodNameObserveValue, null, null);
		//
		IObservableValue methodReturnTypeObserveValue = BeansObservables.observeValue(modelData, "returnType");
		IObservableValue txtReturnTypeObserveTextObserveWidget = SWTObservables.observeText(txtReturnType, SWT.Modify);
		bindingContext.bindValue(txtReturnTypeObserveTextObserveWidget, methodReturnTypeObserveValue, new StringToClassUpdateValueStrategy(), new ClassToStringUpdateValueStrategy());
		//
		parametersComposite.setMethodClass(modelData.getClass());
		parametersComposite.setMethodParameters(modelData.getParameters());
		return bindingContext;
	}
}
